package org.jeecg.modules.business.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.entity.BusHotel;
import org.jeecg.modules.business.entity.BusRoomPayType;
import org.jeecg.modules.business.mapper.BusHotelMapper;
import org.jeecg.modules.business.service.IBusHotelService;
import org.jeecg.modules.rooms.entity.CesAllDayPriceRule;
import org.jeecg.modules.rooms.service.CesAllDayPriceRuleServiceImpl;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: bus_hotel
 * @Author: jeecg-boot
 * @Date:   2023-02-14
 * @Version: V1.0
 */
@Service
public class BusHotelServiceImpl extends ServiceImpl<BusHotelMapper, BusHotel> implements IBusHotelService {
    @Resource
    private BusHotelMapper busHotelMapper;

    @Resource
    private CesAllDayPriceRuleServiceImpl allDayPriceRuleService;

    @Override
    public Result<?> hotelCheck(BusHotel busHotel) {

        CesAllDayPriceRule findRule = allDayPriceRuleService.getOne(Wrappers.<CesAllDayPriceRule>query()
        .eq("hotel_id",busHotel.getId()));
        // 审核酒店通过时，添加默认的全天放计费规则
        if(findRule == null && busHotel.getCheckStatus().equals(1)) {
            findRule = allDayPriceRuleService.defaultRule();
            findRule.setHotelId(busHotel.getId());
            findRule.setInvalid(false);
            allDayPriceRuleService.save(findRule);
        }
//        this.busHotelMapper.updateById(busHotel);
        this.busHotelMapper.update(busHotel, new LambdaQueryWrapper<BusHotel>().eq(BusHotel::getId, busHotel.getId()));
        return Result.ok("信息审核成功!");
    }

    /**
     * @Description 根据名称模糊查询酒店数据
     * @param name
     * @Author: fendo
     * @return ArrayList<BusHotel>
     */
    public  List<BusHotel> findHotelsByHotelName(String name) {
        //接口自己验证参数是否正确  不相信别人
        if(StringUtils.isBlank(name)) return new ArrayList<>();
        /**
         * @TODO 没有枚举
         */
        List<BusHotel> busHotelList = this.busHotelMapper.selectList(Wrappers.<BusHotel>lambdaQuery()
                .eq(BusHotel::getTenantId, TokenUtils.currentTenantId())
                .eq(BusHotel::getStatus, 1)
                .eq(BusHotel::getCheckStatus, 1)
                .eq(BusHotel::getDelFlag, false)
                .like(BusHotel::getName, CommonUtils.escapeChar(name))
        );
        return busHotelList;
    }

    public List<BusHotel> findHotelsByIds(List<String> ids){
        if(CollectionUtil.isEmpty(ids)) return new ArrayList<>();
        List<BusHotel> list = this.busHotelMapper.selectList(Wrappers.<BusHotel>lambdaQuery()
                .eq(BusHotel::getTenantId, TokenUtils.currentTenantId())
                .eq(BusHotel::getStatus, 1)
                .eq(BusHotel::getCheckStatus, 1)
                .eq(BusHotel::getDelFlag, false)
                .in(BusHotel::getId,ids));
        return list;
    }
    @Override
    public Result<?> editHotelInfo(BusHotel busHotel) {

//        this.busHotelMapper.updateById(busHotel);
        this.busHotelMapper.update(busHotel, new LambdaQueryWrapper<BusHotel>().eq(BusHotel::getId, busHotel.getId()));
        return Result.ok("信息编辑成功!");
    }

    /**
     * 可入住酒店列表
     * @param page
     * @param tenantId
     * @param keyWord
     * @param sort 0推荐排序 1 距离优先 2低价优先 3评分优先 4评论数优先
     * @return
     */
    public Page<BusHotel> pageList(Page<BusHotel> page, String tenantId,String city, String keyWord, Integer sort,BigDecimal lat, BigDecimal lng) {
        return page.setRecords(this.busHotelMapper.pageList(page, tenantId,city, keyWord, sort, lat, lng));
    }
}
